package com.atguigu.day21.obj04;

import org.junit.Test;

import java.io.*;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/26 15:46
 * @注释
 */
public class Exer {
    @Test
    public void test02(){
        //读取数据
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\aaa.txt"))) {
            //按照写入顺序依次读取
            System.out.println("ois.readInt() = " + ois.readInt());
            System.out.println("ois.readDouble() = " + ois.readDouble());
            System.out.println("ois.readBoolean() = " + ois.readBoolean());
            System.out.println("ois.readUTF() = " + ois.readUTF());

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test01(){
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\aaa.txt"))) {
            //写入数据
            oos.writeInt(123);
            oos.writeDouble(1.234);
            oos.writeBoolean(true);
            oos.writeUTF("男儿当自强");
            System.out.println("写入完毕");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
